package cn.e2win.biz.user;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import org.springframework.stereotype.Repository;

import cn.e2win.base.json.vo.Pager;
import cn.e2win.biz.base.BaseDAOHibernateImpl;
import cn.e2win.biz.util.StringUtils;


@Repository
public class CompanyDaoImpl  extends BaseDAOHibernateImpl  implements CompanyDao {
	

	@Override
	public void pagerCompany(Pager pager) {
		String sql = "SELECT * FROM sys_company ";
		
		try {
			pager.processSql(sql);
			processPager(pager);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	@Override
	public Long userCount(int coId) {
		String sql = "SELECT COUNT(1) AS userCount FROM Sys_User WHERE coId = " + coId;
		Map map = super.get1stRecordBySql(sql, new HashMap());
		return (Long)map.get("userCount");
	}
	@Override
	public void pagerDict(Pager pager) throws SQLException {
		String sql = "SELECT * FROM(" +
				" SELECT t1.id AS bigId, t1.myKey AS bigKey, t1.myValue AS bigValue , t1.editAbleInt," +
				" t2.id, t2.myKey, t2.myValue, t2.orderIndex, t2.coId,t2.remark, t2.createById, t2.createDate,t2.activeStatusInt " +
				" FROM sys_Dict t1" +
				" LEFT JOIN sys_Dict t2 ON t1.myKey = t2.myType " +
				" WHERE t1.myKey IN ('PAY_OUT_TYPE','PAY_ACCOUNT') " +//可供查看的只有支出类型及收付款帐号
				") AS t" +
				" WHERE t.myKey IS NOT NULL AND (t.coId is NULL OR t.coId=" + pager.getScMap().get("coId") + ") " +
				//(StringUtils.isEmpty(pager.getScMap().get("editAbleInt")) ? "" : " AND t.editAbleInt=" + pager.getScMap().get("editAbleInt")) +
				(StringUtils.isEmpty(pager.getScMap().get("bigKey")) ? "" : (" AND t.bigKey='" + pager.getScMap().get("bigKey")) + "'") +
				" ORDER BY bigId, orderIndex";


		pager.processSql(sql);
		processPager(pager);
	}

	@Override
	public Long getDictTypeCount(String myType) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("myType", myType);
		Map rMap = super.get1stRecordBySql("SELECT COUNT(1) AS dictTypeCount FROM sys_dict WHERE myType=:myType", map);
		return (Long)rMap.get("dictTypeCount");
	}

}
